package com.molyfun.parents.activity.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.view.SinglineClearEditText;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MolificationPasswordActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.base_title)
    TextView mBaseTitle;

    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.old_password)
    SinglineClearEditText mOldPassword;
    @InjectView(R.id.new_password)
    SinglineClearEditText mNewPassword;
    @InjectView(R.id.confirmation_password)
    SinglineClearEditText mConfirmationPassword;
    @InjectView(R.id.complete_password_confirmation)
    TextView mCompletePasswordConfirmation;

    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, MolificationPasswordActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_molification_password);
        ButterKnife.inject(this);
        mBaseTitle.setText("修改密码");
        mOldPassword.setHint("请输入旧密码...");
        mNewPassword.setHint("请输入新密码...");
        mConfirmationPassword.setHint("请输入新密码...");
        mOldPassword.setIsPwd();
        mNewPassword.setIsPwd();
        mConfirmationPassword.setIsPwd();
        initData();

    }

    private void initData() {
        getSupportActionBar().setTitle("");
        mBack.setOnClickListener(this);
        mCompletePasswordConfirmation.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.complete_password_confirmation:
                String mOldPass = mOldPassword.getString();
                String mNewPass = mNewPassword.getString();
                String mConfirmationPass = mConfirmationPassword.getString();
                if (!TextUtils.isEmpty(mOldPass)) {
                    if (!TextUtils.isEmpty(mNewPass)) {
                        if (!TextUtils.isEmpty(mConfirmationPass)) {
                            if (mNewPass.equals(mConfirmationPass)) {
                                getConfirmationNewPass(mOldPass, mConfirmationPass);
                            } else {
                                toast("新密码与确认密码不同");
                            }
                        } else {
                            toast("确认密码为空");
                        }
                    } else {
                        toast("新密码为空");
                    }
                } else {
                    toast("旧密码为空");
                }
                break;
        }

    }

    private void getConfirmationNewPass(String mOldPass, String mNewAccountNum) {
        showProgressDialog();
        Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().newPassword(mOldPass, mNewAccountNum);
        call.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getCode() == 200) {
//                    toast(response.body().getMsg());
                    toast("修改成功");
                    finish();
                } else {
                    if(!TextUtils.isEmpty(response.body().getMsg())){
                        toast(response.body().getMsg());
                    }else{
                        toast("提交失败");
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                dismissProgressDialog();
                toast("提交异常");
            }
        });

    }
}
